﻿using DotNetExtensions.Core.Restful;
using DotNetExtensions.Core.SqlSugar;
using DotNetExtensions.Core.WebApi.Contollers;
using DotNetExtensions.Core.WebApi.Jwt;
using DotNetExtensions.Core.WebApi.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SqlSugarSimple.Models;
using SqlSugarSimple.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SqlSugarSimple.Controllers
{
    [Area("sugar")]
    [ApiController, Authorize]
    [Route("sugar/[controller]/[action]")]
    public class IndexController : BaseController<PersonModel, UserModel, PersonSearchModel>
    {
        public IndexController(PersonService service) : base(service)
        {

        }

        [HttpGet]
        public ApiResult<List<PersonModel>> GetAll()
        {
            return ApiResult.OK(Service.GetAll());
        }

        [HttpGet, AllowAnonymous]
        public ApiResult<UserModel> Login(string name, string pwd)
        {
            string token = JwtValidateService<UserModel>.CreateJwtToken(new JwtParameter[] {
                new JwtParameter(JwtParameters.Sid, "1"),
                new JwtParameter(JwtParameters.Name, name),
                new JwtParameter(JwtParameters.Role, "admin"),
            }, 1, out string audience, out DateTime expiredTime);
            UserModel userModel = new UserModel()
            {
                Expired = 1,
                ExpiredTime = expiredTime,
                Token = token,
                UserInfo = new UserInfoModel
                {
                    Code = "admin",
                    Name = name,
                    Id = 1
                }
            };
            return ApiResult.OK<UserModel>(userModel);
        }
    }
}
